using System;
using System.IO;

using ICSharpCode.SharpZipLib.Zip;
namespace framework.util
{
    class ZipHandler
    {
        public static void CreateZip(string id, string[] maps, string[] mtrls, string[] shots,
                                     string zipoutput)
        {
            using (ZipOutputStream s = new ZipOutputStream(File.Create(zipoutput)))
            {
                s.SetLevel(0);
                byte[] buffer = new byte[4096];
                foreach (string file in maps)
                {
                    ZipEntry entry = new ZipEntry("map-" + id + "/" + Path.GetFileName(file));
                    entry.DateTime = DateTime.Now;
                    s.PutNextEntry(entry);
                    using (FileStream fs = File.OpenRead(file))
                    {
                        int sourceBytes;
                        do
                        {
                            sourceBytes = fs.Read(buffer, 0, buffer.Length);
                            s.Write(buffer, 0, sourceBytes);
                        } while (sourceBytes > 0);
                    }
                }
                foreach (string file in shots)
                {
                    ZipEntry entry = new ZipEntry("shot-" + id + "/" + Path.GetFileName(file));
                    entry.DateTime = DateTime.Now;
                    s.PutNextEntry(entry);
                    using (FileStream fs = File.OpenRead(file))
                    {
                        int sourceBytes;
                        do
                        {
                            sourceBytes = fs.Read(buffer, 0, buffer.Length);
                            s.Write(buffer, 0, sourceBytes);
                        } while (sourceBytes > 0);
                    }
                }
                foreach (string file in mtrls)
                {
                    ZipEntry entry = new ZipEntry("mtrl/" + Path.GetFileName(file));
                    entry.DateTime = DateTime.Now;
                    s.PutNextEntry(entry);
                    using (FileStream fs = File.OpenRead(file))
                    {
                        int sourceBytes;
                        do
                        {
                            sourceBytes = fs.Read(buffer, 0, buffer.Length);
                            s.Write(buffer, 0, sourceBytes);
                        } while (sourceBytes > 0);
                    }
                }
                s.Finish();
                s.Close();
            }
        }
    }
}
